﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using Framework.Common.Sql;

namespace Diaocha.Controllers
{
    public class HomeController : Controller
    {
        public static string connectionString;
        static HomeController()
        {
            connectionString = Framework.Common.Configs.SystemConfig.GetString("db");
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Login()
        {
            string username = RequestExtention.GetString("username").Trim();
            string password = RequestExtention.GetString("password").Trim();
            if (username != password)
            {
                JsHelper.AlertAndRedirect("账号错误", "/");
                return null;
            }
            DataSet ds = SqlHelper.ExecuteDataset(
                connectionString,
                CommandType.Text,
                string.Format("select * from Staff where SupWWID='{0}' or wwid = '{0}'", username));
            var rows = ds.AsEnumerable();
            if (rows != null)
            {
                int recordCount = rows.Count();
                if (recordCount > 0)//登陆成功
                {
                    Session["userid"] = username;

                    var manager = rows
                        .Where(i => i.Field<string>("SupWWID") == username)
                        .FirstOrDefault();
                    var employee = rows
                        .Where(i => i.Field<string>("wwid") == username && i.Field<string>("SupWWID") != username)
                        .FirstOrDefault();
                    if (manager != null && employee != null)//两个调查
                    {
                        Session["role"] = "choice";
                        return RedirectToAction("choice", "vote");
                    }
                    else if (manager != null)
                    {
                        Session["role"] = "manager";
                        return RedirectToAction("manager", "vote");
                    }
                    else if (employee != null)
                    {
                        Session["role"] = "employee";
                        return RedirectToAction("employee", "vote");
                    }
                }
            }
            JsHelper.AlertAndRedirect("账号错误", "/");
            return null;
        }
    }
}
